﻿using System.Reflection;
using _048_Reflection;
using static System.Console;

/*
 * 反射
 */
Type t = typeof(MyClass);

// 获取所有属性
PropertyInfo[] propertyInfos = t.GetProperties();
WriteLine("MyClass类的属性-Properties:");
foreach (PropertyInfo property in propertyInfos)
{
    WriteLine($"-{property.Name} ({property.PropertyType})");
}

// 获取所有方法
MethodInfo[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
WriteLine("\nMyClass类的方法-Methods");
foreach (MethodInfo method in methods)
{
    WriteLine($"-{method.Name}");
}

// 设置属性值
MyClass myClass = new MyClass();
Type type = typeof(MyClass);
PropertyInfo pInfo = type.GetProperty("Id");

// 给属性赋值
pInfo.SetValue(myClass, 34);
WriteLine($"Id:{pInfo.GetValue(myClass)}");

// 动态加载程序集
Assembly assembly = Assembly.Load("047-FactorySample");

ReadLine();